﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;
using WebBanThuoc.Function;
using System.Collections.Specialized;

namespace WebBanThuoc.admin
{
    public partial class QLTinTuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }



            if (!IsPostBack)
            {

            }
            else
            {
                if (checkDefaultSubmit())
                {
                    string IDTinTuc = Request.Params["IDTinTuc"];
                    if (IDTinTuc != null && IDTinTuc.Trim() != "")
                    {
                        IServerWeb sw = new ServerWeb();
                        TinTuc_DAO daoTT = new TinTuc_DAO(sw.Connection);
                        TinTuc tt = daoTT.getByKey(int.Parse(IDTinTuc));

                        sw.closeConnection();
                        if (tt != null)
                        {
                            Session.Add("page_QLTinTuc_dataTinTuc", tt);

                            NoiDungSoLuoc.Text = tt.NoiDungSoLuoc;
                            NoiDung.Text = tt.NoiDung;
                            
                        }
                        else
                        {
                            NoiDungSoLuoc.Text = "";
                            NoiDung.Text = "";
                            
                        }
                    }
                }
                else
                {
                    if (Request.Params["btn_Them"] != null)
                    {
                        add();
                    }
                    if (Request.Params["btn_Sua"] != null)
                    {
                        update();
                    }
                    if (Request.Params["btn_Xoa"] != null)
                    {
                        delete();
                    }
                }
            }

            this.LoadComplete += new EventHandler(QLTinTuc_LoadComplete);
        }

        void QLTinTuc_LoadComplete(object sender, EventArgs e)
        {
            getDataGeneral();
        }
        private bool checkDefaultSubmit()
        {
            if (Request.Params["btn_Xoa"] != null || Request.Params["btn_Sua"] != null || Request.Params["btn_Them"] != null)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Lấy data mặc định cho page
        /// </summary>
        private void getDataGeneral()
        {
            IServerWeb sw = new ServerWeb();
            DanhMucTinTuc_DAO daoDM = new DanhMucTinTuc_DAO(sw.Connection);
            TinTuc_DAO daoTT = new TinTuc_DAO(sw.Connection);

            List<DanhMucTinTuc> dm = daoDM.getListByKey();
            int idTintuc = daoTT.createIDTinTuc();
            
            Session.Add("page_QLTinTuc_dataListDM", dm);
            Session.Add("page_QLTinTuc_IDTinTucMoi", idTintuc);
            sw.closeConnection();
        }

        private void add()
        {
            string idTinTuc = Request.Params["IDTinTuc"];
            if (idTinTuc != null || idTinTuc.ToString().Trim() == "")
            {
                TinTuc tt = new TinTuc();
                IServerWeb sw = new ServerWeb();
                TinTuc_DAO daoTT = new TinTuc_DAO(sw.Connection);
                KhachHang_DAO daoKH = new KhachHang_DAO(sw.Connection);
                tt.TenTinTuc = Request.Params["TenTinTuc"].ToString();
                tt.NoiDungSoLuoc = NoiDungSoLuoc.Text;
                tt.NoiDung = NoiDung.Text;
                tt.AnhNho = "";
                tt.MaDanhMuc = Convert.ToInt32(Request.Params["MaDanhMuc"]);
                tt.NoiDungGuiEmail = "";
                tt.UserName = FuncAdmin.getDataUserLoginCurrent(Session).UserName;
                //sp.MaSanPham = daoSP.createMaSanPham();
                int tempRS = daoTT.add(tt);
                //tempRS = 1;
                if (tempRS > 0)
                {
                    TinTuc ttNew = daoTT.getByKey(tt.IDTinTuc);
                    Session.Add("page_QLTinTuc_msgSucessFunc", "Đã thêm thành công tin tức mới " + ttNew.IDTinTuc);
                    NoiDung.Text = "";
                    NoiDungSoLuoc.Text = "";

                    //auto send mail

                    List<string> mailBcc = (from d in daoKH.getListByKey() select d.Email).ToList();
                    
                    string path = System.Configuration.ConfigurationManager.AppSettings["AnhTinTuc"].Trim().Replace("..", "");                    
                    Function.Func ff = new Function.Func();
                    //foreach(string ob in mailBcc){

                    //    ff.sendMailNew_One(ob, new List<string>(), new List<string>(), "Happyfamifyshop.vn - Tin Tức mới -" + ttNew.TenTinTuc,
                    //    ttNew.NoiDung.Replace("src=\"" + path, "src=\"" + Request.Url.Authority + path));
                    //}
                    ff.sendMailNew_Many("", new List<string>(), mailBcc, "Happyfamifyshop.vn - Tin Tức mới - " + ttNew.TenTinTuc,
                        ttNew.NoiDung.Replace("src=\"" + path, "src=\"" + Request.Url.Authority + path));
                }
                sw.closeConnection();
            }
        }

        private void update()
        {
            string idTinTuc = Request.Params["IDTinTuc"];
            if (idTinTuc != null || idTinTuc.ToString().Trim() != "")
            {

                IServerWeb sw = new ServerWeb();
                TinTuc_DAO daoTT = new TinTuc_DAO(sw.Connection);
                TinTuc tt = daoTT.getByKey(Convert.ToInt32(idTinTuc));

                tt.IDTinTuc = Convert.ToInt32(idTinTuc);
                tt.TenTinTuc = Request.Params["TenTinTuc"].ToString();
                tt.NoiDungSoLuoc = NoiDungSoLuoc.Text;
                tt.NoiDung = NoiDung.Text;
                tt.AnhNho = "";
                tt.MaDanhMuc = Convert.ToInt32(Request.Params["MaDanhMuc"]);
                tt.NoiDungGuiEmail = "";
                tt.UserName = FuncAdmin.getDataUserLoginCurrent(Session).UserName;
                //sp.MaSanPham = daoSP.createMaSanPham();
                int tempRS = daoTT.updateByKey(tt, tt.IDTinTuc);
                if (tempRS > 0)                
                {
                    TinTuc ttNew = daoTT.getByKey(tt.IDTinTuc);
                    Session.Add("page_QLTinTuc_msgSucessFunc", "Đã Cập nhật thành công tin túc " + ttNew.IDTinTuc);
                    Session.Add("page_QLTinTuc_dataTinTuc", ttNew);
                    NoiDungSoLuoc.Text = ttNew.NoiDungSoLuoc;
                    NoiDung.Text = ttNew.NoiDung;
                }
                sw.closeConnection();
            }
        }

        private void delete()
        {
            string idTintuc = Request.Params["IDTinTuc"];
            if (idTintuc != null || idTintuc.ToString().Trim() != "")
            {

                IServerWeb sw = new ServerWeb();
                TinTuc_DAO daoTT = new TinTuc_DAO(sw.Connection);

                //sp.MaSanPham = daoSP.createMaSanPham();
                int tempRS = daoTT.deleteByKey(int.Parse(idTintuc));
                if (tempRS > 0)
                {
                    Session.Add("page_QLTinTuc_msgSucessFunc", "Đã Xóa thành công tin tức " + idTintuc);
                    NoiDungSoLuoc.Text = "";
                    NoiDung.Text = "";                    
                    
                }
                sw.closeConnection();
            }
        }


    }
}